package utilities;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class MailUtil {
    private String host = ""; // smtp服务器
    private String from = ""; // 发件人地址
    private String to = ""; // 收件人地址
    private String user = ""; // 用户名
    private String pwd = ""; // 密码
    private String subject = ""; // 邮件标题

    public void setAddress(String from, String to, String subject) {
        this.from = from;
        this.to = to;
        this.subject = subject;
    }

    public void send(String host, String user, String pwd, String content) {
        this.host = host;
        this.user = user;
        this.pwd = pwd;

        Properties props = new Properties();

        // 设置发送邮件的邮件服务器的属性（这里使用腾讯的smtp服务器）
        props.put("mail.smtp.host", host);
        // 需要经过授权，也就是有户名和密码的校验，这样才能通过验证（一定要有这一条）
        props.put("mail.smtp.auth", "true");
        // 设置端口号，使用SSL
        props.put("mail.smtp.port", 587);

        // 用刚刚设置好的props对象构建一个session
        Session session = Session.getDefaultInstance(props);

        // 有了这句便可以在发送邮件的过程中在console处显示过程信息，供调试使
        // 用（你可以在控制台（console)上看到发送邮件的过程）
        session.setDebug(true);

        // 用session为参数定义消息对象
        MimeMessage message = new MimeMessage(session);
        try {
            // 加载发件人地址
            message.setFrom(new InternetAddress(from));
            // 加载收件人地址
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            // 加载标题
            message.setSubject(subject);

            // 向multipart对象中添加邮件的各个部分内容，包括文本内容和附件
            Multipart multipart = new MimeMultipart();

            // 设置邮件的文本内容
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setText(content);
            multipart.addBodyPart(contentPart);

            // 将multipart对象放到message中
            message.setContent(multipart);
            // 保存邮件
            message.saveChanges();
            // 发送邮件
            Transport transport = session.getTransport("smtp");
            // 连接服务器的邮箱
            transport.connect(host, user, pwd);
            // 把邮件发送出去
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean send(String contact, String vcode) {
        System.out.println("Start");
        try {
            MailUtil mailUtil = new MailUtil();
            mailUtil.setAddress("842642463@qq.com", contact, "二手帮验证码");
            mailUtil.send("smtp.qq.com",
                    "842642463@qq.com",
                    "vgcjcfjbvexdbfib",
                    "您的二手帮验证码为 " + vcode + " 请及时输入");
            System.out.println("Finished");
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public static boolean sendPassword(String contact, String pswd) {
        try {
            MailUtil mailUtil = new MailUtil();
            mailUtil.setAddress("842642463@qq.com", contact, "二手帮账户密码");
            mailUtil.send("smtp.qq.com",
                    "842642463@qq.com",
                    "vgcjcfjbvexdbfib",
                    "您忘记的二手帮密码是: " + pswd + " 请愉快地登录吧！");
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public static boolean sendBuy(String buyer,String buyername,String seller,String sellername,String title) {
        try {
            MailUtil mailUtil = new MailUtil();
            mailUtil.setAddress("842642463@qq.com", buyer, "二手帮：商品购买成功");
            mailUtil.send("smtp.qq.com",
                    "842642463@qq.com",
                    "vgcjcfjbvexdbfib",
                    "您成功购买了卖家[" + sellername + "]的["+title+"]，请联系卖家邮箱进一步沟通: "+seller);

            MailUtil mailUtil2 = new MailUtil();
            mailUtil2.setAddress("842642463@qq.com", seller, "二手帮：商品出售成功");
            mailUtil2.send("smtp.qq.com",
                    "842642463@qq.com",
                    "vgcjcfjbvexdbfib",
                    "您出售的[" + title + "]已经被买家["+buyername+"]购买，请联系买家邮箱进一步沟通: "+buyer);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}